// pages/setting/index.js
const { request }  = require('../../request/index.js');
const ImageSource=  require('../../utils/image_path.js');
const crypto = require("../../utils/test");

const app=getApp();
var nameHistory='';
Page({

    data: {
        NavbarHeight:app.globalData.navBarHeight + app.globalData.statusBarHeight,
        WindowWidth:app.globalData.windowWidth,
        ScreenHeight:app.globalData.ScreenHeight,
        navigationSetting:{
            title:'设置',
            model:1,
        },
        isActive:0,
        sexArray:['未填写','男','女'],
        sexIndex:0,
        date:app.globalData.nowTime.substring(0,10),
        personData: {name:"张三",},
        region: [],
        customItem: '',
        icon:[],
        userData:[],
        imageIndex:0,
        addressData:[],
    },
    onLoad(option){
        console.log(JSON.parse(decodeURIComponent(option.data)))
        const data=JSON.parse(decodeURIComponent(option.data));
        this.setData({
            userData:data
        })
        setTimeout(()=>{
            this.onloadIcon();
        },200)
    },
    onloadIcon(){
        var that=this;
        let fileList=[];
        for(let i=0;i<ImageSource.imageList[7].length;i++){
            fileList.push(ImageSource.Base+ImageSource.page[7]+ImageSource.imageList[7][i])
        }
        let iconUrl=[];
        new Promise((resolve =>
                wx.cloud.getTempFileURL({
                    fileList:fileList,
                    success:res=>{
                        console.log(res)
                        for(let i=0;i<res.fileList.length;i++){
                            iconUrl.push(res.fileList[i].tempFileURL)
                        }
                        resolve(iconUrl)
                    }
                })
        )).then(r=>{
            that.setData({
                icon:r,
                imageIndex:iconUrl.indexOf(that.data.userData.image)
            })
        })
        console.log(that.data.icon)
    },
    boxClickItemTap(e){
      const index=e.currentTarget.dataset.index
      if(index==5){
          var message=this.data.userData.address.split("@");
          this.setData({
              addressData:message,
              region:[message[3],message[4],message[5]]
          })
          if(message[3]==null){
              this.setData({
                  region:[0,0,0]
              })
          }
      }
      this.setData({
          isActive:index
      })
    },
    maskCloseItemTap(){
        this.setData({
            isActive:0
        })
    },
    //图标
    iconSelectItemTap(){
        let l="userData.image"
        this.setData({
            [l]:this.data.icon[this.data.imageIndex],
            isActive:0,
        })
        app.globalData.userDataChangeFlag=1;
        //wx.setStorageSync("userData",this.data.userData)
        this.updateData();

    },
    iconSelectCloseItemTap(){
        this.setData({
            isActive:0,
            imageIndex:this.data.icon.indexOf(this.data.userData.image)
        })
    },
    imageChangeItemTap(e){
        console.log(e);
        const index=this.data.imageIndex;
        this.setData({
            imageIndex:e.currentTarget.dataset.index
        })
    },
    //昵称
    nickNameBindFocus(e){
        nameHistory=e.detail.value
        console.log(nameHistory)
    },
    nickNameInputAction(e){
        if(e.currentTarget.dataset.name==""){
            console.log("错误1")
            let name = "userData.nickName"
            this.setData({
                [name]:nameHistory,
                'personData.name':nameHistory
            })
            wx.showToast({
                title: '昵称不应为空',
                icon: 'none',
                duration: 1500,
            })
            return

        }
        let name = e.currentTarget.dataset.name
        this.setData({
            [name]: e.detail.value.trim().replace(/[^a-zA-Z0-9\u4e00-\u9fa5]/g,""),
            'userData.nickName':e.detail.value.trim().replace(/[^a-zA-Z0-9\u4e00-\u9fa5]/g,""),// 或者用trim(可以过滤掉两边的空格)
        })

    },
    nickNameBlurAction(e){
        if(e.currentTarget.dataset.name==""){
            console.log("错误2")
            let name = "userData.nickName"
            this.setData({
                [name]:nameHistory,
                'personData.name':nameHistory
            })
            wx.showToast({
                title: '昵称不应为空',
                icon: 'none',
                duration: 1500,
            })
            return
        }
        if(e.detail.value.length>6){
            let name = e.currentTarget.dataset.name
            this.setData({
                [name]:nameHistory,
                'userData.nickName':nameHistory
            })
            wx.showToast({
                title: '昵称长度应该小于7',
                icon: 'none',
                duration: 1500,
            })
        }
        else{
            app.globalData.userDataChangeFlag=1;
            //wx.setStorageSync("userData",this.data.userData)
            this.updateData();
        }
    },
    nickNameConfirm(e){
        if(e.currentTarget.dataset.name==""){
            console.log("错误3")
            let name = "userData.nickName"
            this.setData({
                [name]:nameHistory,
                'personData.name':nameHistory
            })
            wx.showToast({
                title: '昵称不应为空',
                icon: 'none',
                duration: 1500,
            })
            return
        }
        if(e.detail.value.length>6){
            console.log(e.detail.value.length)
            let name = e.currentTarget.dataset.name
            this.setData({
                [name]:nameHistory,
                'personData.name':nameHistory
            })
            wx.showToast({
                title: '昵称长度应该小于7',
                icon: 'none',
                duration: 1500,
            })
        }else{
            app.globalData.userDataChangeFlag=1;
            //wx.setStorageSync("userData",this.data.userData)
            this.updateData();
        }
    },
    bindPickerChange: function(e) {
        let l="userData.sex"
        this.setData({
            [l]: ++e.detail.value
        })
        console.log(this.data.userData.sex)
        app.globalData.userDataChangeFlag=1;
        //wx.setStorageSync("userData",this.data.userData)
        this.updateData();

    },
    bindDateChange: function(e) {
        let l="userData.date"
        this.setData({
            [l]: e.detail.value
        })
        app.globalData.userDataChangeFlag=1;
        //wx.setStorageSync("userData",this.data.userData)
        this.updateData();
    },
    bindRegionChange: function(e){
        this.setData({
            region: e.detail.value
        })
        let l1="addressData[3]"
        let l2="addressData[4]"
        let l3="addressData[5]"
        this.setData({
            [l1]:this.data.region[0],
            [l2]:this.data.region[1],
            [l3]:this.data.region[2]
        })
    },
    sexRadioChange(e){
        console.log(e)
        if(e.detail.value=="man"){
            let l="addressData[1]"
            this.setData({
                [l]:1
            })
        }else{
            let l="addressData[1]"
            this.setData({
                [l]:2
            })
        }
    },
    returnPage(){
        wx.navigateBack({
            delta: 1
        });
    },
    updateData(){
        let params={
            url:'/user/update',
            method: 'PUT',
            header: {
                "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
            },
            data: {
                data:crypto.AES_EN(JSON.stringify(this.data.userData))
            },
        }
        request(params);
    },
    addressSelectItemTap(){
        console.log(JSON.stringify(this.data.addressData));
        let l="userData.address"
        this.setData({
            [l]:this.data.addressData.join('@'),
            isActive:0,
        })
        app.globalData.userDataChangeFlag=1;
        console.log(this.data.userData.address);

        this.updateData();
    },
    addressInputAction(e){
        let l="addressData[6]";
        this.setData({
            [l]:e.detail.value
        })
    },
    phoneInputAction(e){
        let l="addressData[2]";
        this.setData({
            [l]:e.detail.value
        })
    },
    nameInputAction(e){
        let l="addressData[0]";
        this.setData({
            [l]:e.detail.value
        })
    },
    addressSelectCloseItemTap(){
        this.setData({
            isActive:0,
        })
    }
})